package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.UpdateInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.module.bbs.client.entity.BbsContent;
import com.meida.module.bbs.provider.service.BbsContentService;
import com.meida.module.file.client.entity.SysFile;
import com.meida.module.file.provider.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 帖子更新
 *
 * @author zyf
 */
@Slf4j
@Component("bbsContentUpdateHandler")
public class BbsContentUpdateHandler implements UpdateInterceptor<BbsContent> {

    @Autowired
    private BbsContentService bbsContentService;

    @Autowired
    private SysFileService sysFileService;

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BbsContent bbsContent) {
        Long contentId = bbsContent.getContentId();
        BbsContent newBbsContent = bbsContentService.getById(contentId);
        Long parentId = newBbsContent.getParentId();
        //不是空的话
        if (FlymeUtils.isNotEmpty(parentId)) {
            BbsContent oldBbsContent = bbsContentService.getById(parentId);
            if (FlymeUtils.isNotEmpty(oldBbsContent)) {
                //删除原帖子
                bbsContentService.removeById(parentId);
                //删除原附件
                log.info("删除原附件parentId为" + parentId);
                sysFileService.deleteByBusId(parentId);
                log.info("备份contentId为" + contentId);
                String fileIds = params.get("fileIds");
                String coverImg = sysFileService.getCoverImg(fileIds);
                //更新备份帖子Id为原Id
                CriteriaUpdate cu1 = new CriteriaUpdate();
                cu1.eq("contentId", contentId);
                cu1.set("contentState", 1);
                cu1.set("contentId", parentId);
                cu1.set("parentId", null);
                cu1.set("coverImage", coverImg);
                bbsContentService.update(cu1);
               /* //关联新附件（前端调删除附件接口了无用）
                List<SysFile> sysFileList = selectFileList(contentId);
                log.info("sysFileList长度" + sysFileList.size());
                if (sysFileList.size() > 0) {
                    sysFileList.forEach(sysFile -> {
                        sysFile.setBusId(parentId);
                    });
                    sysFileService.updateBatchById(sysFileList);
                }*/
                bbsContent.setContentId(parentId);

            }
        }
    }


    private List<SysFile> selectFileList(Long contentId) {
        CriteriaQuery cq = new CriteriaQuery(BbsContent.class);
        cq.eq(true, "busId", contentId);
        cq.eq(true, "busType", BbsContent.class.getSimpleName());
        return sysFileService.list(cq);
    }



}
